﻿using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.ServiceModel.Syndication;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using Cofe.Core;
using Cofe.Web.Feed;
using Cofe.Web.Interfaces;
using Cofe.Web.Utils;

namespace Cofe.Web.Implements
{
    [Export(typeof(ICofeService))]
    [ServicePriority(ServicePriorityAttribute.DefaultPriority_COFEWS)]
    public class FeedResponseFormatter : IHttpResponseMessageFormatter
    {
        public bool CanCreateResponse(HttpRequestMessage request,
            object returnVal, ParameterDic pd, ResponseFormatterInfo info)
        {
            return (returnVal is IFeed || returnVal is SyndicationItem || returnVal is SyndicationFeed);
        }

        public HttpResponseMessage CreateResponse(HttpRequestMessage request,
            object returnVal, ParameterDic pd, ResponseFormatterInfo info)
        {
            HttpResponseMessage response;

            if (returnVal is SyndicationItem)
                response = request.CreateResponse<SyndicationItem>(info.StatusCode, returnVal as SyndicationItem);
            else if (returnVal is SyndicationFeed)
            {
                
                response = request.CreateResponse<SyndicationFeed>(info.StatusCode, returnVal as SyndicationFeed);
            }
            else
            {
                IFeed feed = returnVal as IFeed;
                response = request.CreateResponse<SyndicationFeed>(info.StatusCode, feed.InnerFeed);
               
            }

            return response;
        }
    }
}
